'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Interface ITcpContext
    Sub Connect(ByVal endPoint As System.Net.IPEndPoint)

    Sub Disconnect()

    Sub SendData(ByVal data As List(Of Byte))

    Sub SetCloseExpected()

    Sub SetCloseUnexpected()

    ReadOnly Property BytesReceived() As Long

    ReadOnly Property BytesSent() As Long

    ReadOnly Property Id() As String

    ReadOnly Property IsConnected() As Boolean

    ReadOnly Property ConnectionCloseExpected() As Boolean

    Event Connected As EventHandler(Of ConnectedEventArgs)

    Event Disconnected As EventHandler(Of DisconnectEventArgs)

    Event DataReceived As EventHandler(Of DataReceivedEventArgs)
End Interface

Public Class ConnectedEventArgs
    Inherits EventArgs

    Private ReadOnly _EndPoint As System.Net.IPEndPoint

    Public Sub New(ByVal endPoint As System.Net.IPEndPoint)
        _EndPoint = endPoint
    End Sub

    Public ReadOnly Property EndPoint() As System.Net.IPEndPoint
        Get
            Return _EndPoint
        End Get
    End Property
End Class

Public Class DisconnectEventArgs
    Inherits EventArgs

    Private ReadOnly _IsExpected As Boolean

    Public Sub New(ByVal expected As Boolean)
        _IsExpected = expected
    End Sub

    Public ReadOnly Property IsExpected() As Boolean
        Get
            Return _IsExpected
        End Get
    End Property
End Class

Public Class DataReceivedEventArgs
    Inherits EventArgs

    Private Shared ticketCounter As Integer

    Private ReadOnly _Data As New List(Of Byte)
    Private ReadOnly _Ticket As Integer

    Public Sub New(ByVal data As IEnumerable(Of Byte))
        _Data.AddRange(data)

        _Ticket = Threading.Interlocked.Increment(ticketCounter)
    End Sub

    Public ReadOnly Property Data() As List(Of Byte)
        Get
            Return _Data
        End Get
    End Property

    Public ReadOnly Property Ticket() As Integer
        Get
            Return _Ticket
        End Get
    End Property
End Class